﻿using Main.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Main
{
    public partial class EnglishToChinese : Form
    {
        private WordInfo[] words;
        private int length = 0;
        private int curIndex = 0;
        private int wrongNum = 0;

        private WordTestInfo wordTestInfo = new WordTestInfo();
        public EnglishToChinese(List<WordInfo> words)
        {
            InitializeComponent();
            InitWords(words.ToArray());
        }

        private void btNext_Click(object sender, EventArgs e)
        {
            this.wordTestInfo.SumCount++;
            this.curIndex++;
            this.lbWord.Text = this.words[curIndex].EnglishName;
            if (this.rbWrong.Checked)
            {
                this.wrongNum++;
                this.lbWrongNum.Text = this.wrongNum.ToString();
                //错误加1
                this.wordTestInfo.WrongWords.Add(this.words[curIndex]);
               
            }
            if (this.curIndex == this.length - 1)
            {
                WordTestManager.AddNewWordTest(this.wordTestInfo);
                if (MessageBox.Show("已完成测试，是否复习错题？", "提示", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
                {
                    InitWords(wordTestInfo.WrongWords.ToArray());
                }
                else
                {
                    this.Close();
                }
            }
            SetControl();
        }

        private void EnglishToChinese_Load(object sender, EventArgs e)
        {

            SetControl();
        }

        private void SetControl()
        {
            this.lbAnswer.Text = "";
         //   this.rbRight.Checked = false;
           // this.rbWrong.Checked = false;
            if (this.length > 0)
            {
                if (this.curIndex == this.length - 1)
                {
                    this.btNext.Enabled = false;
                }
            }
            else
            {

                this.btNext.Enabled = false;
            }
        }

        private void btShowAnswer_Click(object sender, EventArgs e)
        {
            this.lbAnswer.Text = this.words[this.curIndex].ChineseName;
        }

        private void InitWords(WordInfo[] words)
        {
            this.words = words;
            if (words != null && words.Length > 0)
            {
                this.length = words.Length;
                this.curIndex = 0;
                this.lbWord.Text = words[curIndex].EnglishName;

                this.wordTestInfo.Date = DateTime.Now;
            }
        }
    }
}
